package cn.iocoder.yudao.module.system.dal.mysql.partnercoupon;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.partnercoupon.vo.PartnerCouponPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.partner.PartnerDO;
import cn.iocoder.yudao.module.system.dal.dataobject.partnercoupon.PartnerCouponDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 合伙人优惠券 Mapper
 *
 * @author 万民匠心
 */
@Mapper
public interface PartnerCouponMapper extends BaseMapperX<PartnerCouponDO> {

    default PageResult<PartnerCouponDO> selectPage(PartnerCouponPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PartnerCouponDO>()
                .eqIfPresent(PartnerCouponDO::getPartnerId, reqVO.getPartnerId())
                .eqIfPresent(PartnerCouponDO::getTemplateId, reqVO.getTemplateId())
                .likeIfPresent(PartnerCouponDO::getName, reqVO.getName())
                .eqIfPresent(PartnerCouponDO::getStatus, reqVO.getStatus())
                .eqIfPresent(PartnerCouponDO::getTotalCount, reqVO.getTotalCount())
                .eqIfPresent(PartnerCouponDO::getTakeCount, reqVO.getTakeCount())
//                .betweenIfPresent(PartnerCouponDO::getStartTime, reqVO.getStartTime())
//                .betweenIfPresent(PartnerCouponDO::getEndTime, reqVO.getEndTime())
//                .betweenIfPresent(PartnerCouponDO::getCreateTime, reqVO.getCreateTime())
//                .eqIfPresent(PartnerCouponDO::getStoreId, reqVO.getStoreId())
                .leIfPresent(PartnerCouponDO::getStartTime, reqVO.getNowTime())
                .geIfPresent(PartnerCouponDO::getEndTime, reqVO.getNowTime())
                .orderByDesc(PartnerCouponDO::getId));
    }
    default void updateTakeCount(Long id, Integer incrCount) {
        update(null, new LambdaUpdateWrapper<PartnerCouponDO>()
                .eq(PartnerCouponDO::getId, id)
                .setSql("take_count = take_count + " + incrCount));
    }

    @Select("SELECT partner_id from system_partner_coupon where start_time <= #{dateTime} and #{dateTime} <= end_time  GROUP BY partner_id")
    List<Long> selectHasSendList(LocalDateTime dateTime);
}